package com.qa.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qa.pojo.Sort;
import com.qa.pojo.Sort_Func;
import com.qa.pojo.TestCase;
import com.qa.pojo.Users;
import com.qa.service.ITestCaseService;

@Controller
@RequestMapping("/testCase")
public class SortsController {
	@Resource
	private ITestCaseService testCaseService;
	Logger logger = Logger.getLogger(this.getClass());

	/**
	 * 获取通用sort分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/getSorts")
	@ResponseBody
	public Map<String, Object> getSorts(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			List<Sort> sorts = this.testCaseService.getSorts();
			List<Sort_Func> re_sort_func = this.testCaseService.getSort_func();
			if (sorts != null && sorts.size() > 0 && re_sort_func != null) {
				resultMap.put("tag", "success");
				resultMap.put("sortList", sorts);
				resultMap.put("functionList", re_sort_func);
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 查询测试用例
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/selectCase")
	@ResponseBody
	public Map<String, Object> selectCase(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int sort = Integer.valueOf(request.getParameter("sort"));
			int func = request.getParameter("func") == null ? 0 : Integer
					.valueOf(request.getParameter("func"));
			List<String> group = request.getParameter("examineGroup") == null ? null
					: Arrays.asList(URLDecoder.decode(
							request.getParameter("examineGroup"), "UTF-8"));

			List<Integer> examineGroup = new ArrayList<Integer>();
			if (group == null || group.size() == 0
					|| group.get(0).trim().equals("")) {
				examineGroup = null;
			} else {
				for (int i = 0; i < group.get(0).split(",").length; i++) {
					examineGroup.add(Integer
							.valueOf(group.get(0).split(",")[i]));
				}
			}
			String caseAscrString = request.getParameter("caseAscr");
			int caseAscr = -1;
			if (caseAscrString.equals("person")) {
				caseAscr = 1;
			} else if (caseAscrString.equals("public")) {
				caseAscr = 0;
			} else {
				caseAscr = -1;
			}
			String selectFunction = request.getParameter("selectFunction")
					.equals("null") ? null : URLDecoder.decode(
					request.getParameter("selectFunction"), "UTF-8");
			String selectKeyword = URLDecoder.decode(
					request.getParameter("selectKeyword"), "UTF-8");
			int usersid = getUser(request).getUsersid();
			List<TestCase> testCases = this.testCaseService.selectCase(sort,
					func, examineGroup, caseAscr, selectFunction,
					selectKeyword, usersid);
			if (testCases != null && testCases.size() > 0) {
				resultMap.put("tag", "success");
				resultMap.put("result", testCases);
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 添加测试用例
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/insertCase", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertCase(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int sortAdd = Integer.valueOf(request.getParameter("sortAdd"));
			int funcAdd = Integer.valueOf(request.getParameter("funcAdd"));

			String funchildname = URLDecoder.decode(
					request.getParameter("funchildname"), "UTF-8");
			String expectresult = URLDecoder.decode(
					request.getParameter("expectresult"), "UTF-8");
			String step = URLDecoder.decode(request.getParameter("step"),
					"UTF-8");
			String remark = URLDecoder.decode(request.getParameter("remark"),
					"UTF-8");
			int usersid = getUser(request).getUsersid();

			int isSuccess = this.testCaseService.insertTestCase(sortAdd,
					funcAdd, funchildname, step, expectresult, remark, usersid);
			if (isSuccess == 1) {
				resultMap.put("tag", "success");
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 编辑测试用例
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/EditCase", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> EditCase(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			int editCaseId = Integer
					.valueOf(request.getParameter("editCaseId"));
			int sortEdit = Integer.valueOf(request.getParameter("sortEdit"));
			int funcEdit = Integer.valueOf(request.getParameter("funcEdit"));

			String funchildnameEdit = URLDecoder.decode(
					request.getParameter("funchildnameEdit"), "UTF-8");
			String expectresultEdit = URLDecoder.decode(
					request.getParameter("expectresultEdit"), "UTF-8");
			String stepEdit = URLDecoder.decode(
					request.getParameter("stepEdit"), "UTF-8");
			String remarkEdit = URLDecoder.decode(
					request.getParameter("remarkEdit"), "UTF-8");

			int isSuccess = this.testCaseService.editTestCase(editCaseId,
					sortEdit, funcEdit, funchildnameEdit, stepEdit,
					expectresultEdit, remarkEdit);

			if (isSuccess == 1) {
				resultMap.put("tag", "success");
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 删除测试用例
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/deleteCase", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteCase(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {
			List<String> signGroup = request.getParameter("signGroup") == null ? null
					: Arrays.asList(URLDecoder.decode(
							request.getParameter("signGroup"), "UTF-8"));

			List<Integer> signGroupList = new ArrayList<Integer>();
			if (signGroup == null || signGroup.size() == 0
					|| signGroup.get(0).trim().equals("")) {
				signGroup = null;
			} else {
				for (int i = 0; i < signGroup.get(0).split(",").length; i++) {
					signGroupList.add(Integer.valueOf(signGroup.get(0).split(
							",")[i]));
				}
			}

			int isSuccess = this.testCaseService.deleteTestCase(signGroupList);

			if (isSuccess > 0) {
				resultMap.put("tag", "success");
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 测试用例提交审核
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/examineCase", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> examineCase(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(200);
		try {

			int caseId = Integer.valueOf(request.getParameter("examineCaseId"));
			int isSuccess = this.testCaseService.examineTestCase(caseId);

			if (isSuccess == 1) {
				resultMap.put("tag", "success");
			} else {
				resultMap.put("tag", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return resultMap;
	}

	/**************
	 * 从session中得到用户
	 * 
	 * @param res
	 * @return
	 */
	public static Users getUser(HttpServletRequest res) {
		Users user = (Users) res.getSession().getAttribute("user");
		return user;
	}
}